package com.mobisystems.youpark;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonObject;
import com.mobisystems.youpark.utils.UtilityFunctions;
import com.parse.FunctionCallback;
import com.parse.ParseCloud;
import com.parse.ParseException;
import com.parse.ParseUser;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class RequestConfirmationActivity extends Activity {

	private TextView tvClaimSpotText;
	private Button btnAcceptRequest;
	private Button btnDeclineRequest;
	
	private String requestingUserObjectId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_request_confirmation);
		
		
		Intent pushNotificationIntent = getIntent();
		Log.d(Application.TAG, pushNotificationIntent.toString());
		Bundle extras = pushNotificationIntent.getExtras();
		Log.d(Application.TAG, extras.toString());
		String result = (String) extras.get("com.parse.Data");
		
		
		setupCustomActionBar();
		
		setupComponents();
		
		setUpOnClickListeners(pushNotificationIntent, extras, result);
		
		receivePushNotificationAndSetData(pushNotificationIntent, extras, result);
	}

	private void setUpOnClickListeners(final Intent pushNotificationIntent, final Bundle extras, final String result) {
		btnAcceptRequest.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String objectId = requestingUserObjectId;
				
				
				double amount = 0;
				String currency = null;
				String customerId = null;
				try {
					JSONObject jsonData = new JSONObject(pushNotificationIntent.getExtras().getString("com.parse.Data"));
					
					amount = UtilityFunctions.convertDollarsToCents(jsonData.getString("amount"));
					currency = jsonData.getString("currency");
					customerId = jsonData.getString("customerId");
				} catch (JSONException e1) {
					Log.d(Application.TAG, "JSONException: " + e1.getMessage());
				}
				
				Map<String, Object> chargeCreditCardParams = new HashMap<String, Object>();
				chargeCreditCardParams.put("Amount", "500");
				chargeCreditCardParams.put("Currency", currency);
				chargeCreditCardParams.put("Customer", customerId);
				ParseCloud.callFunctionInBackground("chargeCustomer", chargeCreditCardParams , new FunctionCallback<Object>() {
					public void done(Object result, ParseException e) {
						if(e == null) {
							//Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_SHORT).show();
							Toast.makeText(getApplicationContext(), "User has been notified of space acceptance.", Toast.LENGTH_SHORT).show();
							Intent intent = new Intent(RequestConfirmationActivity.this, MyMap.class);
							startActivity(intent);
							finish();
						} else {
							Log.d(Application.TAG, "chargeCreditCard: " + e.getMessage());
						}
					}
				});
			}
		});
		
		btnDeclineRequest.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "Decline", Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(RequestConfirmationActivity.this, MyMap.class);
				startActivity(intent);
				finish();
			}
		});
		
	}

	private void receivePushNotificationAndSetData(Intent pushNotificationIntent, Bundle extras, String result) {
		
		
		JSONObject jsonData;
		String alert = null;
		String obj = null;
		try {
			jsonData = new JSONObject(pushNotificationIntent.getExtras().getString("com.parse.Data"));
			
			Iterator iterator = jsonData.keys();
			
			while(iterator.hasNext()) {
				String key = (String) iterator.next();
				
				if(key.equals("objectId")) {
					obj = jsonData.getString(key);
					requestingUserObjectId = obj;
					tvClaimSpotText.setText(jsonData.getString("FullName") + " is claiming your spot for " + jsonData.getString("amount")+"$");
				}
			}
			
			Log.d(Application.TAG, "ObjectId is: " + obj);
			
		} catch (JSONException e1) {
			Log.d(Application.TAG, "JSONException: " + e1.getMessage());
		}
	}

	private void setupCustomActionBar() {
		ActionBar customActionBar = getActionBar();
		customActionBar.setDisplayShowHomeEnabled(false);
		customActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.custom_actionbar, null);
        TextView titleTextView = (TextView) view.findViewById(R.id.title_text);
        titleTextView.setText("YouPark");
        view.findViewById(R.id.button_settings).setVisibility(View.INVISIBLE);
        view.findViewById(R.id.button_switch_modes).setVisibility(View.INVISIBLE);
        customActionBar.setCustomView(view);
        customActionBar.setDisplayShowCustomEnabled(true);
	}

	private void setupComponents() {
		tvClaimSpotText = (TextView) findViewById(R.id.tvClaimSpotInfo);
		btnAcceptRequest = (Button) findViewById(R.id.btnAcceptRequest);
		btnDeclineRequest = (Button) findViewById(R.id.btnDeclineRequest);	
	}
}
